package com.bwie.realtime.jtp.dwd.log.job;

import com.alibaba.fastjson.JSON;

import com.bwie.realtime.jtp.dwd.log.bean.PageViewBean;
import com.bwie.realtime.jtp.dwd.log.function.PageViewBeanMapFunction;
import com.bwie.realtime.jtp.dwd.log.function.PageViewWindowFuncition;
import net.bwie.realtime.jtp.utils.DorisUtil;
import net.bwie.realtime.jtp.utils.KafkaUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

import java.time.Duration;

public class JtpTrafficPageViewMinuteWindowDwsJob {


    public static void main(String[] args) throws Exception {
        // 1.执行环境-env
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env.enableCheckpointing(3000L);
        // 2.数据源-source
        DataStream<String> pageStream = KafkaUtil.consumerKafka(env, "dwd-traffic-page-log");
//        pageStream.print("page");
        // 3.数据转换-transformation
        DataStream<String> resultStream = handle(pageStream);
        resultStream.print("resultStream");
        // 4.数据输出-sink
        DorisUtil.saveToDoris(resultStream, "jtp_realtime_report", "dws_traffic_page_view_window_report");
        // 5.触发执行-execute
        env.execute("JtpTrafficPageViewMinuteWindowDwsJob");
    }

    private static DataStream<String> handle(DataStream<String> stream) {
        KeyedStream<String, String> midStream =  stream.keyBy(
                json -> JSON.parseObject(json).getJSONObject("common").getString("mid")
        );

        SingleOutputStreamOperator<PageViewBean> beanStream =  midStream.map(new PageViewBeanMapFunction());
        SingleOutputStreamOperator<PageViewBean> timeStream =  beanStream.assignTimestampsAndWatermarks(
                WatermarkStrategy
                        .<PageViewBean>forBoundedOutOfOrderness(Duration.ofSeconds(0))
                        .withTimestampAssigner(
                                new SerializableTimestampAssigner<PageViewBean>() {
                                    @Override
                                    public long extractTimestamp(PageViewBean pageViewBean, long l) {
                                        return pageViewBean.getTs();
                                    }
                                }
                        )
        );

        KeyedStream<PageViewBean,String> KeyedStream  =  timeStream.keyBy(
                bean->bean.getBrand()+","+bean.getChannel()+","+bean.getProvince()+","+bean.getIsNew()
        );
//        开窗
        WindowedStream<PageViewBean,String, TimeWindow> windowStream=KeyedStream.window(
                TumblingEventTimeWindows.of(Time.minutes(1))
        );
//        聚合
        DataStream<String>  reportStream  =   windowStream.apply(new PageViewWindowFuncition());

//        返回结果
        return reportStream;
    }



}
